package com.zfh;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@SuppressWarnings("serial")
@WebServlet("/test")
public class TestServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("This is a test Servlet<br>");
		out.println("用户的提交方式是："+request.getMethod()+"<br>");
		out.println("<a href='valid'>让session过期</a><br>");
		System.out.println("这里是servlet的工作部分");
		
		// 下面，将创建session
		HttpSession session = request.getSession();
		long craeteTime = session.getCreationTime();
		long lastAccessedTime = session.getLastAccessedTime();
		int keepTime= session.getMaxInactiveInterval();
		System.out.println("拿到的sessionid是："+session.getId());
		System.out.println("该session的创建时间是："+new Date(craeteTime));
		System.out.println("该session的最后访问时间是："+new Date(lastAccessedTime));
		System.out.println("该seesion的默认最长发呆保持时间是："+keepTime+"秒");
		session.setMaxInactiveInterval(5);  // 重置为5秒过期
		keepTime = session.getMaxInactiveInterval();
		System.out.println("重置后，该seesion的最长发呆保持时间是："+keepTime+"秒");
		boolean isNew = session.isNew();
		if (isNew) {
			System.out.println("这是一个新创建的session");
		} else {
			System.out.println("这不是一个新创建的session");
		}
		
	}

}
